﻿Imports System.Xml.Serialization
Imports System.IO
Imports System.Xml
Imports System.Threading
Imports MyIPBssLogic.AppConfig

Namespace AppConfig

    Public Class ConfMngmnt

        Public Shared Function Load(ByRef FileConf As String) As Configuration

            Dim serializer As New XmlSerializer(GetType(Configuration))

            ' Deserialize the object, and close the xmlreader
            Try

                If File.Exists(FileConf) = True Then

                    ' Create a FileStream to Load with.
                    Dim Reader As New FileStream(FileConf, _
                                      FileMode.Open)
                    Dim rd As XmlReader = XmlReader.Create(Reader)

                    Dim Conf As Configuration = serializer.Deserialize(rd)

                    Conf.ConfLoaded = True

                    rd.Close()
                    Reader.Close()


                    Return Conf

                End If

            Catch ex As Exception

            End Try

            Return New Configuration

        End Function

        Public Shared Function Save(ByRef FileConf As String, ByRef Conf As Configuration) As Boolean

            Dim serializer As New XmlSerializer(GetType(Configuration))
            Dim Loaded As Boolean = Conf.ConfLoaded

            Try

                Conf.ConfLoaded = False
                ' Create a FileStream to write with.
                Dim writer As FileStream

                If File.Exists(FileConf) Then
                    writer = New FileStream(FileConf, FileMode.Truncate)
                Else
                    writer = New FileStream(FileConf, FileMode.Create)
                End If

                ' Serialize the object, and close the TextWriter
                serializer.Serialize(writer, Conf)
                writer.Close()

                Save = True
            Catch ex As Exception

            End Try

            Conf.ConfLoaded = Loaded

        End Function

    End Class

    Public Class Configuration

        Public ConfLoaded As Boolean = False
        Public MyIPMailConf As New MailConfig
        Public MyIPAppConfigData As New AppConfigData

        Public Sub New()

        End Sub

    End Class

End Namespace
